<?php
namespace Components\Account\User;

class Validation extends \Phalcon\Validation {
  public function execute($data) {
    $validationMessages = $this -> validate($data);
    if($validationMessages -> valid()) {
      return ['code'=>-1,'message'=>$validationMessages[0]->getMessage()];
    }

    $resData = [];
    foreach($this -> getValidators() as $v) {
      if(isset($data[$v[0]]) && !empty($data[$v[0]])) {
        $filterFunName = 'check' . ucfirst($v[0]);
        if(!strstr($filterFunName, 'Confirm')) {
          if(!method_exists(Adapter::getFilterInstance(), $filterFunName)) { return ['code'=>-1,'message'=>'请联系开发人员:'.get_class(Adapter::getFilterInstance()) . '->' . $filterFunName . '不存在!']; }
          $resData[$v[0]] = Adapter::getFilterInstance() -> $filterFunName($data[$v[0]]);
        }
      }
    }

    return ['code'=>0,'data'=>$resData];
  }

  public function addRealName() {
    return $this -> rules('real_name',[
      new \Phalcon\Validation\Validator\PresenceOf()
    ]);
    return $this;
  }

  public function addCaptcha($parameter = []) {
    return $this -> rules('captcha',[
      new \Phalcon\Validation\Validator\PresenceOf()
    ]);
    return $this;
  }

  public function addPhone() {
    return $this -> rules('phone',[
      new \Phalcon\Validation\Validator\PresenceOf(),
      new \Phalcon\Validation\Validator\StringLength([
        'min' => 11
      ])
    ]);
  }

  public function addPhoneUniqueness() {
    return $this -> rules('phone',[
      new \Phalcon\Validation\Validator\PresenceOf(),
      new \Phalcon\Validation\Validator\StringLength([
        'min' => 11
      ]),
      new \Phalcon\Validation\Validator\Uniqueness([
        'model' => new Model(),
        'message' => '手机号已存在'
      ])
    ]);
  }

  public function addPasswordPresenceOf() {
    return $this -> rules('password',[
      new \Phalcon\Validation\Validator\PresenceOf(),
      new \Phalcon\Validation\Validator\StringLength([
        'min' => 6
      ])
    ]);
  }

  public function addPassword() {
    return $this -> rules('password', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '密码不能少于6位',
        'callback' => function($data) {
          if(!isset($data['password']) || empty($data['password'])) {
            return true;
          }

          if(strlen($data['password']) < 6) {
            return false;
          }

          return true;
        }
      ])
    ]);
  }

  public function addConfirmPassword() {
    return $this -> rules('confirm_password', [
      new \Phalcon\Validation\Validator\Confirmation([
        'message' => '确认密码不一致',
        'with' => 'password'
      ]),
      new \Phalcon\Validation\Validator\Callback([
        'message' => '确认密码不能少于6位',
        'callback' => function($data) {
          if(!isset($data['confirm_password']) || empty($data['confirm_password'])) {
            return true;
          }

          if(strlen($data['confirm_password']) < 6) {
            return false;
          }

          return true;
        }
      ])
    ]);
  }

  public function addSafePasswordPresenceOf() {
    return $this -> rules('safe_password',[
      new \Phalcon\Validation\Validator\PresenceOf(),
      new \Phalcon\Validation\Validator\StringLength([
        'min' => 6
      ])
    ]);
  }

  public function addSafePassword() {
    return $this -> rules('safe_password', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '安全密码不能少于6位',
        'callback' => function($data) {
          if(!isset($data['safe_password']) || empty($data['safe_password'])) {
            return true;
          }

          if(strlen($data['safe_password']) < 6) {
            return false;
          }

          return true;
        }
      ])
    ]);
  }

  public function addConfirmSafePassword() {
    return $this -> rules('confirm_safe_password', [
      new \Phalcon\Validation\Validator\Confirmation([
        'message' => '安全确认密码不一致',
        'with' => 'password'
      ]),
      new \Phalcon\Validation\Validator\Callback([
        'message' => '安全确认密码不能少于6位',
        'callback' => function($data) {
          if(!isset($data['confirm_safe_password']) || empty($data['confirm_safe_password'])) {
            return true;
          }

          if(strlen($data['confirm_safe_password']) < 6) {
            return false;
          }

          return true;
        }
      ])
    ]);
  }

  public function addUserLevel() {
    return $this -> rules('user_level',[
      new \Phalcon\Validation\Validator\InclusionIn([
        'domain' => [1, 2, 3, 4, 5, 6]
      ])
    ]);
  }

  public function addShopLevel() {
    return $this -> rules('shop_level',[
      new \Phalcon\Validation\Validator\InclusionIn([
        'domain' => [1, 2, 3]
      ])
    ]);
  }

  public function addStatus() {
    return $this -> rules('status',[
      new \Phalcon\Validation\Validator\InclusionIn([
        'domain' => [0, 1, 2]
      ])
    ]);
  }
}
